import { useState, useCallback } from 'react';
import { detail } from '@/api';

export default function useDetail() {
  //上中下游产业
  const [chainList, setChainList] = useState<any>({});
  const getChainList = useCallback(
    async (params: { industrial_chain_id: string }) => {
      const res = await detail.getChainList(params);
      if (res && res.data) {
        setChainList(res.data);
      } else {
        setChainList({});
      }
    },
    [],
  );

  //数据看板
  const [dataBoard, setDataBoard] = useState([]);
  const getDataBoard = useCallback(
    async (params: { industrial_chain_id: string }) => {
      const res = await detail.getDataBoard(params);
      if (res && res.data) {
        setDataBoard(res.data);
      } else {
        setDataBoard([]);
      }
    },
    [],
  );

  //数据看板 历史趋势图
  const [dataBoardBar, setDataBoardBar] = useState<any>({});
  const getDataBoardBar = useCallback(
    async (params: { industrial_chain_id: string; type: string }) => {
      const res = await detail.getDataBoardBar(params);
      if (res && res.data) {
        setDataBoardBar(res.data);
      } else {
        setDataBoardBar({});
      }
    },
    [],
  );

  const [dataBoardPie, setDataBoardPie] = useState<any>({});
  const getDataBoardPie = useCallback(
    async (params: { industrial_chain_id: string; type: string }) => {
      const res = await detail.getDataBoardPie(params);
      if (res && res.data) {
        setDataBoardPie(res.data);
      } else {
        setDataBoardPie({});
      }
    },
    [],
  );

  //企业名单
  const [entList, setEntList] = useState([]);
  const getEntList = useCallback(
    async (params: {
      industrial_chain_parent: string;
      industrial_chain_no: string;
      processes_type: number;
      processes_no: string;
    }) => {
      const res = await detail.getEntList(params);
      if (res && res.data) {
        setEntList(res.data);
      } else {
        setEntList([]);
      }
    },
    [],
  );

  //产业动态
  const [trends, setTrends] = useState<any>({});
  const getTrends = useCallback(
    async (params: {
      Type: number;
      pageNo: number | string;
      pageSize: number;
      industrial_chain_id: string;
    }) => {
      const res = await detail.getTrends(params);
      if (res) {
        setTrends(res);
      } else {
        setTrends({});
      }
    },
    [],
  );

  return {
    dataBoard,
    getDataBoard,
    entList,
    getEntList,
    chainList,
    getChainList,
    trends,
    getTrends,
    dataBoardBar,
    getDataBoardBar,
    dataBoardPie,
    getDataBoardPie,
  };
}
